package com.jingtuo.android.common.app;

import java.lang.Thread.UncaughtExceptionHandler;

import com.jingtuo.android.R;
import com.jingtuo.android.common.Constants;
import com.jingtuo.android.common.db.SqliteHelper;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

public class BaseApp extends Application implements UncaughtExceptionHandler{
	
	public static final String TAG = BaseApp.class.getName();
	
	protected UncaughtExceptionHandler mDefaultExceptionHandler;
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		initAppExceptionHandler();
		new Thread(){
			public void run() {
				sendServer();
			};
		}.start();
	}
	
	public void initAppExceptionHandler(){
		mDefaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(this);
	}
	
	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
		// TODO Auto-generated method stub
		if(ex==null){
			mDefaultExceptionHandler.uncaughtException(thread, ex);
		}else{//存储异常信息
			save(ex);
			System.exit(0);
		}
	}
	
	/**
	 * 保存异常信息
	 * @param ex
	 */
	public void save(Throwable ex){
		SharedPreferences preferences = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
		SqliteHelper.saveExceptionInfo(preferences.getString(Constants.KEY_DB_PATH, ""), ex);
	}
	
	/**
	 * 将日志信息发送给服务器
	 */
	public void sendServer(){
		
	}
	
	
	
	
}
